using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Samples.DirectX.UtilityToolkit;
using Microsoft.DirectX.Direct3D;
using System.Drawing;
using Microsoft.DirectX;

namespace Noein
{
    /// <summary>
    /// Rendering object wrapper.
    /// </summary>
    public class Visual
    {
        private bool visible = true;
        public bool Visible
        {
            get { return visible; }
            set { visible = value; }
        }

        public virtual void Render(Device device)
        {
            // do nothing
        }
    }

    /// <summary>
    /// Render with framework mesh.
    /// </summary>
    public class FrameworkMeshVisual : Visual
    {
        private FrameworkMesh mesh;
        
        public FrameworkMeshVisual(FrameworkMesh mesh)
        {
            this.mesh = mesh;
        }

        public override void Render(Device device)
        {
            // HACK otherwise the model is black, even with lights
            device.RenderState.Lighting = false;
            mesh.Render(device);
        }
    }

    public class MeshVisual : Visual
    {
        private Mesh mesh;

        public Mesh Mesh
        {
            get { return mesh; }
            set { mesh = value; }
        }

        private Material material;

        public Material Material
        {
            get { return material; }
            set { material = value; }
        }

        public MeshVisual(Mesh mesh, Material material)
        {
            this.mesh = mesh;
            this.material = material;
        }

        public override void Render(Device device)
        {
            device.Material = material;
            mesh.DrawSubset(0);
        }
    }

    public class SpriteVisual : Visual
    {
        private Sprite sprite;
        private Texture texture;

        public SpriteVisual(Device device, string srcFile)
        {
            sprite = new Sprite(device);
            texture = TextureLoader.FromFile(device, srcFile);
        }

        public override void Render(Device device)
        {   
            // HACK fix the scaling issue
            Matrix m = sprite.Transform;
            m.Scale(.001f, .001f, .001f);
            sprite.Transform = m;

            sprite.SetWorldViewLH(device.Transform.World, device.Transform.View);
            sprite.Begin(SpriteFlags.ObjectSpace | SpriteFlags.Billboard | SpriteFlags.AlphaBlend | SpriteFlags.SortDepthBackToFront);
            sprite.Draw(texture, Rectangle.Empty, Vector3.Empty, Vector3.Empty, Color.White);
            sprite.End();
        }
    }
}
